package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/17 17:07
 **/
public class Day32_2_Num61_RotateRight {
    class Solution {
        public ListNode rotateRight(ListNode head, int k) {
            if(head == null || head.next == null) return head;
            ListNode cur = head, newHead;
            int size = 0;
            while(cur != null) {cur = cur.next;size++;}
            k = k % size;
            while(k-- > 0) {         // 链表后移k次
                cur = head;
                while(cur.next.next != null) cur = cur.next; // 找到倒数第二个节点
                newHead = cur.next;  // 得到倒数第一个节点
                cur.next = null;     // 倒数第一个节点置空
                newHead.next = head; // 倒数第一个节点作为首节点
                head = newHead;
            }
            return head;
        }
    }
}